// pages/viewWaybill/viewWaybill.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var tip = require("../../utils/tip");
var modelData = require("../../utils/data");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    startDate: '开始日期',
    startDateView: false,
    endDate: '结束日期',
    endDateView: false,
    show: false,
    current: 1,
    size: 10,
    totalPage: 0,
    item: {}
  },

  startDateChange(e) {
    this.setData({
      startDate: e.detail.value,
      startDateView: true
    })
    if (this.data.startDate != '开始日期' && this.data.endDate != '结束日期') {
      this.setData({
        current: 1,
        list: [],
      })
      this.getWaybillList()
    }
  },
  endDateChange(e) {
    this.setData({
      endDate: e.detail.value,
      endDateView: true
    })
    if (this.data.startDate != '开始日期' && this.data.endDate != '结束日期') {
      this.setData({
        current: 1,
        list: [],
      })
      this.getWaybillList()
    }
  },

  reset() {
    this.setData({
      startDate: "开始日期",
      startDateView: false,
      endDate: '结束日期',
      endDateView: false,
      current: 1,
      list: [],
    })
    this.getWaybillList()
  },

  cancelDispatch(e) {
    let item = e.currentTarget.dataset.item
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认对运单【' + item.waybillNumber + '】取消派车？',
      success: async function (res) {
        if (res.confirm) {
          let requestUrl = url.cancelPlanDispatch + "?waybillNumber=" + item.waybillNumber
          let data = await commons.postJson(requestUrl);
          if (data.code == 200) {
            tip.showToast(data.msg, 'success', 1000)
            that.getWaybillList(1)
          } else {
            tip.showToast(data.msg, 'error', 1000)
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  goWaybillDetail(e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: "../waybillDetail/waybillDetail?waybillNumber=" + item.waybillNumber + '&path=historicalWaybill',
    })
  },

  goWaybillBookingDetail(e) {
    let waybillNumber = e.currentTarget.dataset.waybillnumber
    wx.navigateTo({
      url: "../waybillBookingDetail/waybillBookingDetail?waybillNumber=" + waybillNumber,
    })
  },

  getWaybillList(current) {
    if (current == 1) {
      this.setData({
        list: [],
        current: current
      })
    }
    let data = {
      current: this.data.current,
      size: this.data.size,
      driverPhone: this.data.item.driverPhone
    }
    
    let requestUrl = url.fleetCaptainWaybillTable;
    commons.get(requestUrl, data).then((res) => {
      let resData = res.data
      resData.records.forEach((item, index) => {
        item.waybillStatusFont = commons.getFont(modelData.ysStatus, item.waybillStatus)
        resData.records.splice(index, 1, item)
      });
      this.setData({
        list: this.data.list.concat(resData.records),
        show: true,
        totalPage: Math.ceil(resData.total / this.data.size)
      })
    }).catch(err => {
      // console.log(err, '0000');
    });
  },

  loadMore(e) {
    console.log('加载更多')
    if (this.data.totalPage === 1) {
      return
    }

    if (this.data.current < this.data.totalPage) {
      this.setData({
        current: this.data.current + 1,
      })
      this.getWaybillList()
      console.log(this.data.current, '当前页')
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    if (e.item) {
      this.setData({
        item: JSON.parse(e.item)
      })
    }
    console.log(e, 'e000')
    this.getWaybillList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      current: 1,
      list: []
    })
    this.getWaybillList()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})